/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javase - T6
 * <p>Powered by webrx On 2021-12-24 14:31:47
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class T7 {
    static CountDownLatch latch = new CountDownLatch(1);

    static class T {

        public List<String> list = new ArrayList<>();

        void add() {
            list.add("item" + list.size());
            System.out.printf("%d = %s%n", list.size(), list.get(list.size() - 1));
        }

        boolean count() {
            boolean f = false;
            if (list.size() >= 5) {
                System.out.printf("%s 线程启动...", Thread.currentThread().getName());
                f = true;
            }
            return f;
        }
    }

    public static void main(String[] args) {
        T t = new T();
        var add = new Thread(() -> {
            for (int i = 0; i < 20; i++) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                    t.add();
                    if (t.list.size() >= 5) latch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        add.start();

        var count = new Thread(() -> {
            try {
                latch.await();
                System.out.println("容器已经添加到5个元素...");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }, "监控线程");
        count.start();
    }
}
